#include "../include/N_arytree.h"
#include "../include/errorcodes.h"


void move_file(char *source_file_path,char *dest_file_path) 
{

	char *path;
	path=(char*)malloc(sizeof(char)*50);
	
	fd *source;

	source=bstSearchNode(&bstRoot,source_file_path);                //search from nary changed to search in bst

	if(source==NULL)
	{
		printf("movefile_FAILURE %s\n",ERR_VFS_MOVEFILE_01);
		return;
	}
	
	path=parsePath(dest_file_path);

	fd *dest=bstSearchNode(&bstRoot,path);	
	
	if(dest==NULL)
	{
		printf("movefile_FAILURE %s\n",ERR_VFS_MOVEFILE_02);
		return;
	}
	
	//printf("\npath in filemove beffore return=%s\n",dest_file_path);
	//printf("\npath in filemove after parsepath*******=%s\n",path);
	

	if(source->fileType == 'D') {
		printf("movefile_FAILURE %s\n", ERR_VFS_MOVEFILE_01);
		return;
	}
	
	char *newFileName = fetchFileName(dest_file_path);

	dest = bstSearchNode(&bstRoot,dest_file_path);
	if(dest != NULL) {
		delete_node(&naryRoot,dest_file_path);
		hashDelete(hash,*dest);
		bstDeleteNode(&bstRoot,dest_file_path);	
	}

	fd *new= (fd *)malloc(sizeof(fd));
	strcpy(new->file_name,newFileName);
	strcpy(new->path,path);
	new->blockNum=source->blockNum;
	new->fileType='F';
	new->fileSize=source->fileSize;
	
	delete_node(&naryRoot,source_file_path);
	hashDelete(hash,*source);
	bstDeleteNode(&bstRoot,source_file_path);	

//	printf("\npath in filemove after parsepath 2*******=%s\n",source_file_path);
	insert_node(&naryRoot,new);
	hashInsert(hash,*new);
	bstInsertNode(&bstRoot,new);
	

//	fd newHash=*new;
//	path=parsePath(dest_file_path);
//	updateHashFileDesc(hash,new,path);

//	printf("\nNary print*****************************\n");
//	printtree(naryRoot);
	
	printf("movefile_SUCCESS\n");
}
